package com.bolingcavalry.rabbitmq;

import com.alibaba.fastjson.JSONObject;
import com.winit.cloudlink.event.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

/**
 * Created by wanglin on 2018/5/16.
 */
@Component
@Lazy(false)
public class TestListener1 extends EventListener {
    @Autowired
    private static Logger logger = LoggerFactory.getLogger(TestListener1.class);
    @Value("${cloudlink.EventName}")
    private String EventName;

    @Override
    public String getEventName() {
        return EventName;
    }

    @Override
    public void onCreate(CreateEvent event1) throws EventException {

        String eventId = event1.getHeaders().getEventId();
        String module = event1.getHeaders().getModule();

        try {
            JSONObject json= (JSONObject) event1.getPayload();
            System.out.println(json.toString());
            this.getEventEngine().newEventBuilder()
                    .eventType(getEventName())
                    .refEventId(eventId )
                    .module(module)
                    .eventOperation(EventOperation.Failure)
                    .payload(json)
                    .build();

        } catch (Exception e) {
            logger.info(e.getMessage());
//             发送处理失败的状态事件
            JSONObject json= (JSONObject) event1.getPayload();
            this.getEventEngine().newEventBuilder()
                    .eventType(getEventName())
                    .refEventId(eventId )
                    .module(module)
                    .eventOperation(EventOperation.Failure)
                    .payload(json)
                    .build();
        }

    }

    @Override
    public void onRetry(RetryEvent event) throws EventException {
    }
}
